<!DOCTYPE html><html lang="cn" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>es入门 | HustleLr</title><meta name="keywords" content="中间件"><meta name="author" content="HustleLr"><meta name="copyright" content="HustleLr"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1. ElasticSearch基本概念1.1 ES索引1一个索引由一个名字来标识（必须全部是小写字母的），并且当我们要对对应于这 个索引中的文档进行索引、搜索、更新和删除的时候，都要使用到这个名字 1.2 文档（Document）12ES中文档是所有可搜索数据的最小单元，ES的文档就像MySql中的一条记录，只是ES的文档会被序列化成json格式，保存在Elasticsearch中，这个json">
<meta property="og:type" content="article">
<meta property="og:title" content="es入门">
<meta property="og:url" content="http://greenfivebird.gitee.io/hustleblog/2022/03/01/es%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="HustleLr">
<meta property="og:description" content="1. ElasticSearch基本概念1.1 ES索引1一个索引由一个名字来标识（必须全部是小写字母的），并且当我们要对对应于这 个索引中的文档进行索引、搜索、更新和删除的时候，都要使用到这个名字 1.2 文档（Document）12ES中文档是所有可搜索数据的最小单元，ES的文档就像MySql中的一条记录，只是ES的文档会被序列化成json格式，保存在Elasticsearch中，这个json">
<meta property="og:locale">
<meta property="og:image" content="http://greenfivebird.gitee.io/hustleblog/img/es.jfif">
<meta property="article:published_time" content="2022-03-01T11:57:15.000Z">
<meta property="article:modified_time" content="2022-03-01T11:58:24.796Z">
<meta property="article:author" content="HustleLr">
<meta property="article:tag" content="中间件">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://greenfivebird.gitee.io/hustleblog/img/es.jfif"><link rel="shortcut icon" href="/hustleblog/img/head.jpg"><link rel="canonical" href="http://greenfivebird.gitee.io/hustleblog/2022/03/01/es%E5%85%A5%E9%97%A8/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/hustleblog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/hustleblog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'es入门',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-03-01 19:58:24'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.1"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/hustleblog/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/hustleblog/archives/"><div class="headline">Articles</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/hustleblog/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a></div><div class="data-item"><a href="/hustleblog/categories/"><div class="headline">Categories</div><div class="length-num">12</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hustleblog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/link/"><i class="fa-fw fas fa-link"></i><span> 外链</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/hustleblog/img/es.jfif')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/hustleblog/">HustleLr</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hustleblog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/link/"><i class="fa-fw fas fa-link"></i><span> 外链</span></a></div><div class="menus_item"><a class="site-page" href="/hustleblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">es入门</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-03-01T11:57:15.000Z" title="Created 2022-03-01 19:57:15">2022-03-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-03-01T11:58:24.796Z" title="Updated 2022-03-01 19:58:24">2022-03-01</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/hustleblog/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="es入门"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="1-ElasticSearch基本概念"><a href="#1-ElasticSearch基本概念" class="headerlink" title="1. ElasticSearch基本概念"></a>1. ElasticSearch基本概念</h3><h4 id="1-1-ES索引"><a href="#1-1-ES索引" class="headerlink" title="1.1 ES索引"></a>1.1 ES索引</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">一个索引由一个名字来标识（必须全部是小写字母的），并且当我们要对对应于这 个索引中的文档进行索引、搜索、更新和删除的时候，都要使用到这个名字</span><br></pre></td></tr></table></figure>
<h4 id="1-2-文档（Document）"><a href="#1-2-文档（Document）" class="headerlink" title="1.2 文档（Document）"></a>1.2 文档（Document）</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ES中文档是所有可搜索数据的最小单元，ES的文档就像MySql中的一条记录，只是ES的文档会被序列化成json格式，保存在Elasticsearch中，</span><br><span class="line">这个json对象是由字段组成，字段就相当于Mysql的列，每个字段有自己的类型，如果不去指定会自动匹配类型，文档的JSON格式支持嵌套与数组</span><br></pre></td></tr></table></figure>
<h4 id="1-3-字段类型"><a href="#1-3-字段类型" class="headerlink" title="1.3  字段类型"></a>1.3  字段类型</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">每一个字段都应该有一个对应的类型，例如：Text、Keyword、Byte等</span><br></pre></td></tr></table></figure>
<h3 id="2-ES基本操作"><a href="#2-ES基本操作" class="headerlink" title="2. ES基本操作"></a>2. ES基本操作</h3><h4 id="2-1-常用索引操作"><a href="#2-1-常用索引操作" class="headerlink" title="2.1 常用索引操作"></a>2.1 常用索引操作</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1) 创建索引	格式: PUT /索引名称</span><br><span class="line">	举例: PUT /es_db</span><br><span class="line">2) 查询索引	格式: GET /索引名称</span><br><span class="line">	举例: GET /es_db</span><br><span class="line">3) 删除索引	格式: DELETE /索引名称</span><br><span class="line">	举例: DELETE /es_db</span><br></pre></td></tr></table></figure>
<h4 id="2-2-常用文档操作"><a href="#2-2-常用文档操作" class="headerlink" title="2.2 常用文档操作"></a>2.2 常用文档操作</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">1) 添加文档	格式: PUT /索引名称/类型/id</span><br><span class="line">	举例:	PUT /es_db/_doc/1</span><br><span class="line">		 &#123;</span><br><span class="line">			 &quot;name&quot;: &quot;张三&quot;,</span><br><span class="line">			 sex&quot;: 1</span><br><span class="line">		 &#125;</span><br><span class="line">2) 修改文档 格式: PUT /索引名称/类型/id（同添加文档格式，注意id）</span><br><span class="line">3) 查询文档	格式: GET /索引名称/类型/id</span><br><span class="line">	举例: GET /es_db/_doc/1</span><br><span class="line">4) 删除文档	格式: DELETE /索引名称/类型/id</span><br><span class="line">	举例: DELETE /es_db/_doc/1</span><br></pre></td></tr></table></figure>
<h4 id="2-3-常用查询ES内容操作（可使用postman或浏览器直接查询）"><a href="#2-3-常用查询ES内容操作（可使用postman或浏览器直接查询）" class="headerlink" title="2.3 常用查询ES内容操作（可使用postman或浏览器直接查询）"></a>2.3 常用查询ES内容操作（可使用postman或浏览器直接查询）</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">1) 查询当前类型中的所有文档 _search</span><br><span class="line">	格式: GET /索引名称/类型/_search</span><br><span class="line">    举例: GET /es_db/_doc/_search</span><br><span class="line">    </span><br><span class="line">2) 条件查询, 如要查询age等于28岁的 _search?q=*:***</span><br><span class="line">	 格式: GET /索引名称/类型/_search?q=*:***</span><br><span class="line">     举例: GET /es_db/_doc/_search?q=age:28</span><br><span class="line">     </span><br><span class="line">3) 范围查询, 如要查询age在25至26岁之间的 _search?q=***[** TO **] 注 意: TO 必须为大写</span><br><span class="line">	格式: GET /索引名称/类型/_search?q=***[25 TO 26]</span><br><span class="line">	举例: GET /es_db/_doc/_search?q=age[25 TO 26]</span><br><span class="line">	</span><br><span class="line">4) 根据多个ID进行批量查询 _mget</span><br><span class="line">	格式: GET /索引名称/类型/_mget</span><br><span class="line">	举例: GET /es_db/_doc/_mget</span><br><span class="line">		 &#123;</span><br><span class="line">		 &quot;ids&quot;:[&quot;1&quot;,&quot;2&quot;]</span><br><span class="line">		 &#125;</span><br><span class="line">	SQL: select * from student where id in (1,2)</span><br><span class="line">	</span><br><span class="line">5) 查询年龄小于等于28岁的 :&lt;=</span><br><span class="line">	格式: GET /索引名称/类型/_search?q=age:&lt;=**</span><br><span class="line">	举例: GET /es_db/_doc/_search?q=age:&lt;=28</span><br><span class="line">	</span><br><span class="line">6) 查询年龄大于28前的 :&gt;</span><br><span class="line">	格式: GET /索引名称/类型/_search?q=age:&gt;**</span><br><span class="line">	举例: GET /es_db/_doc/_search?q=age:&gt;28</span><br><span class="line">7) 分页查询 from=*&amp;size=*</span><br><span class="line">	格式: GET /索引名称/类型/_search?q=age[25 TO 26]&amp;from=0&amp;size=1</span><br><span class="line">	举例: GET /es_db/_doc/_search?q=age[25 TO 26]&amp;from=0&amp;size=1</span><br><span class="line">	SQL: select * from student where age between 25 and 26 limit 0, 1</span><br><span class="line">	</span><br><span class="line">8) 对查询结果只输出某些字段 _source=字段,字段</span><br><span class="line">	格式: GET /索引名称/类型/_search?_source=字段,字段</span><br><span class="line">	举例: GET /es_db/_doc/_search?_source=name,age</span><br><span class="line">	SQL: select name,age from student</span><br><span class="line">	</span><br><span class="line">9) 对查询结果排序 sort=字段:desc/asc</span><br><span class="line">	格式: GET /索引名称/类型/_search?sort=字段 desc</span><br><span class="line">	举例: GET /es_db/_doc/_search?sort=age:desc</span><br><span class="line">	SQL: select * from student order by age desc</span><br></pre></td></tr></table></figure>
<h4 id="2-4-DSL语言高级查询"><a href="#2-4-DSL语言高级查询" class="headerlink" title="2.4 DSL语言高级查询"></a>2.4 DSL语言高级查询</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1.Query DSL概述 Domain Specific Language 领域专用语言</span><br><span class="line">Elasticsearch provides a ful1 Query DSL based on JSON to define queries </span><br><span class="line">Elasticsearch提供了基于JSON的DSL来定义查询。 DSL由叶子查询子句和复合查询子句两种子句组成。</span><br></pre></td></tr></table></figure>
<p><img src="C:\Users\gaop\AppData\Roaming\Typora\typora-user-images\image-20211221103419742.png" alt="image-20211221103419742"></p>
<h4 id="2-5-term与match"><a href="#2-5-term与match" class="headerlink" title="2.5    term与match"></a>2.5    term与match</h4><p>1、通过term 和 match查询数据时细节点以及数据类型keyword与text区别 </p>
<p><strong>1.1 term查询</strong> </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">term查询keyword字段。</span><br><span class="line">term不会分词。而keyword字段也不分词。需要完全匹配才可。 </span><br><span class="line">hello world === hello world hello world </span><br><span class="line"></span><br><span class="line">term查询text字段。</span><br><span class="line">因为text字段会分词，而term不分词，所以term查询的条件必须是text字段分词后的某一个。</span><br></pre></td></tr></table></figure>
<p><strong>1.2.match查询</strong> </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">match查询keyword字段</span><br><span class="line">match会被分词，而keyword不会被分词，match的需要跟keyword的完全匹配可以。</span><br><span class="line"></span><br><span class="line">match查询text字段</span><br><span class="line">match分词，text也分词，只要match的分词结果和text的分词结果有相同的就匹配。</span><br></pre></td></tr></table></figure>
<p>*我们一般规定：全文检索字段用 match,其他非text字段匹配用term</p>
<h3 id="3-JavaAPI整合ES"><a href="#3-JavaAPI整合ES" class="headerlink" title="3. JavaAPI整合ES"></a>3. JavaAPI整合ES</h3><h4 id="3-1-java集成ES代码示例"><a href="#3-1-java集成ES代码示例" class="headerlink" title="3.1 java集成ES代码示例"></a>3.1 java集成ES代码示例</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">/* 1.构建查询事件请求 param: 索引库名称*/</span><br><span class="line">SearchRequest searchRequest = new SearchRequest(THREAT_INDEX);</span><br><span class="line">/* 构建搜索条件 */</span><br><span class="line">SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();</span><br><span class="line">/* 分页 */</span><br><span class="line">sourceBuilder.from(getPageNum() - 1) * getPageSize());</span><br><span class="line">sourceBuilder.size(getPageSize());</span><br><span class="line">/* 排序 */</span><br><span class="line">sourceBuilder.sort(new FieldSortBuilder(&quot;timestamp.keyword&quot;).order(SortOrder.DESC));</span><br><span class="line">/* 精确查询 */</span><br><span class="line">BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();</span><br><span class="line">boolQueryBuilder.must(QueryBuilders.termQuery(&quot;class1.keyword&quot;, getCategory1()));</span><br><span class="line">/* 范围查询 */</span><br><span class="line">boolQueryBuilder.must(QueryBuilders.rangeQuery(&quot;timestamp.keyword&quot;).from(startDate, true).to(endDate, false));</span><br><span class="line">sourceBuilder.query(boolQueryBuilder);</span><br><span class="line">/* 配置超时时间 */</span><br><span class="line">sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));</span><br><span class="line">/* 执行搜索 */</span><br><span class="line">searchRequest.source(sourceBuilder);</span><br><span class="line">/* 调用查询 */</span><br><span class="line">final SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);</span><br><span class="line">/* 数据解析 */</span><br><span class="line">final SearchHit[] hits = searchResponse.getHits().getHits();</span><br><span class="line">for (SearchHit hit : hits) &#123;</span><br><span class="line">	//获取索引文档数据</span><br><span class="line">	final Map&lt;String, Object&gt; stringObjectMap = hit.getSourceAsMap();</span><br><span class="line">	//获取文档字段</span><br><span class="line">	final Object alert = stringObjectMap.get(&quot;alert&quot;);</span><br><span class="line">	//获取ESID</span><br><span class="line">	String esId = hit.getId();</span><br><span class="line">&#125;</span><br><span class="line">//获取count条数</span><br><span class="line">CountRequest countRequest = new CountRequest(THREAT_INDEX);</span><br><span class="line">final CountRequest countRequest1 = countRequest.query(sourceBuilder.query());</span><br><span class="line">final CountResponse countResponse = highLevelClient.count(countRequest1, RequestOptions.DEFAULT);</span><br><span class="line">Long count = countResponse.getCount();</span><br></pre></td></tr></table></figure>
<h4 id="3-2-精确查询A"><a href="#3-2-精确查询A" class="headerlink" title="3.2 精确查询A"></a>3.2 精确查询A</h4><p>精确，指的是查询关键字（或者关键字分词后），必须与目标分词结果完全匹配。</p>
<ol>
<li>指定字符串作为关键词查询，关键词支持分词</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//查询title字段中，包含 ”开发”、“开放&quot; 这个字符串的document；相当于把&quot;浦东开发开放&quot;分词了，再查询；</span><br><span class="line">QueryBuilders.queryStringQuery(&quot;开发开放&quot;).defaultField(&quot;title&quot;);</span><br><span class="line">//不指定feild，查询范围为所有feild</span><br><span class="line">QueryBuilders.queryStringQuery(&quot;青春&quot;);</span><br><span class="line">//指定多个feild</span><br><span class="line">QueryBuilders.queryStringQuery(&quot;青春&quot;).field(&quot;title&quot;).field(&quot;content&quot;);</span><br></pre></td></tr></table></figure>
<ol>
<li>以关键字“开发开放”，关键字不支持分词</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">QueryBuilders.termQuery(&quot;title&quot;, &quot;开发开放&quot;)</span><br><span class="line">QueryBuilders.termsQuery(&quot;fieldName&quot;, &quot;fieldlValue1&quot;,&quot;fieldlValue2...&quot;)</span><br></pre></td></tr></table></figure>
<ol>
<li>以关键字“开发开放”，关键字支持分词</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">QueryBuilders.matchQuery(&quot;title&quot;, &quot;开发开放&quot;)</span><br><span class="line">QueryBuilders.multiMatchQuery(&quot;fieldlValue&quot;, &quot;fieldName1&quot;, &quot;fieldName2&quot;, &quot;fieldName3&quot;)</span><br></pre></td></tr></table></figure>
<h4 id="3-3-模糊查询"><a href="#3-3-模糊查询" class="headerlink" title="3.3 模糊查询"></a>3.3 模糊查询</h4><p>模糊，是指查询关键字与目标关键字可以模糊匹配。</p>
<ol>
<li>左右模糊查询，其中fuzziness的参数作用是在查询时，es动态的将查询关键词前后增加或者删除一个词，然后进行匹配</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">QueryBuilders.fuzzyQuery(&quot;title&quot;, &quot;开发开放&quot;).fuzziness(Fuzziness.ONE)</span><br></pre></td></tr></table></figure>
<ol>
<li>前缀查询，查询title中以“开发开放”为前缀的document；</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">QueryBuilders.prefixQuery(&quot;title&quot;, &quot;开发开放&quot;)</span><br></pre></td></tr></table></figure>
<ol>
<li>通配符查询，支持*和？，？表示单个字符；注意不建议将通配符作为前缀，否则导致查询很慢</li>
</ol>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">QueryBuilders.wildcardQuery(&quot;title&quot;, &quot;开*放&quot;)</span><br><span class="line">QueryBuilders.wildcardQuery(&quot;title&quot;, &quot;开？放&quot;)</span><br></pre></td></tr></table></figure>
<p><strong>注意，</strong><br>在分词的情况下，针对fuzzyQuery、prefixQuery、wildcardQuery不支持分词查询，即使有这种doucment数据，也不一定能查出来，因为分词后，不一定有“开发开放”这个词；</p>
<h4 id="3-3-范围查询"><a href="#3-3-范围查询" class="headerlink" title="3.3 范围查询"></a>3.3 范围查询</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">//闭区间查询</span><br><span class="line">QueryBuilders.rangeQuery(&quot;fieldName&quot;).from(&quot;fieldValue1&quot;).to(&quot;fieldValue2&quot;);</span><br><span class="line">//开区间查询，默认是true，也就是包含</span><br><span class="line">QueryBuilders.rangeQuery(&quot;fieldName&quot;).from(&quot;fieldValue1&quot;).to(&quot;fieldValue2&quot;).includeUpper(false).includeLower(false);</span><br><span class="line">//大于</span><br><span class="line">QueryBuilders.rangeQuery(&quot;fieldName&quot;).gt(&quot;fieldValue&quot;);</span><br><span class="line">//大于等于</span><br><span class="line">QueryBuilders.rangeQuery(&quot;fieldName&quot;).gte(&quot;fieldValue&quot;);</span><br><span class="line">//小于</span><br><span class="line">QueryBuilders.rangeQuery(&quot;fieldName&quot;).lt(&quot;fieldValue&quot;);</span><br><span class="line">//小于等于</span><br><span class="line">QueryBuilders.rangeQuery(&quot;fieldName&quot;).lte(&quot;fieldValue&quot;);</span><br></pre></td></tr></table></figure>
<h4 id="3-4-多个关键字组合查询boolQuery"><a href="#3-4-多个关键字组合查询boolQuery" class="headerlink" title="3.4 多个关键字组合查询boolQuery()"></a>3.4 多个关键字组合查询boolQuery()</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">QueryBuilders.boolQuery()</span><br><span class="line">QueryBuilders.boolQuery().must();//文档必须完全匹配条件，相当于and</span><br><span class="line">QueryBuilders.boolQuery().mustNot();//文档必须不匹配条件，相当于not</span><br><span class="line">QueryBuilders.boolQuery().should();//至少满足一个条件，这个文档就符合should，相当于or</span><br></pre></td></tr></table></figure>
<h4 id="3-5-聚合操作"><a href="#3-5-聚合操作" class="headerlink" title="3.5 聚合操作"></a>3.5 聚合操作</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">SearchRequest searchRequest = new SearchRequest(THREAT_INDEX);</span><br><span class="line">//ES组名</span><br><span class="line">String aggName = &quot;term_bind&quot;;</span><br><span class="line">SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();</span><br><span class="line">//聚合（按照type类型分组）</span><br><span class="line">TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms(aggName).field(type).size(1000).order(BucketOrder.count(false));</span><br><span class="line">searchSourceBuilder.aggregation(termsAggregationBuilder);</span><br><span class="line">searchRequest.source(searchSourceBuilder);</span><br><span class="line">SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);</span><br><span class="line">//获取分组后的数据集合</span><br><span class="line">Terms termBinds = searchResponse.getAggregations().get(aggName);</span><br><span class="line">List&lt;? extends Terms.Bucket&gt; buckets = termBinds.getBuckets();</span><br><span class="line">//遍历取值</span><br><span class="line">for (Terms.Bucket bucket : buckets) &#123;</span><br><span class="line">	//类型</span><br><span class="line">	Object key = bucket.getKey();</span><br><span class="line">	//个数</span><br><span class="line">	Object value = bucket.getDocCount();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-ES常见问题"><a href="#4-ES常见问题" class="headerlink" title="4.ES常见问题"></a>4.ES常见问题</h3><h4 id="4-1-Es的聚合，统计查询时报错fielddata-true问题"><a href="#4-1-Es的聚合，统计查询时报错fielddata-true问题" class="headerlink" title="4.1 Es的聚合，统计查询时报错fielddata=true问题"></a>4.1 Es的聚合，统计查询时报错fielddata=true问题</h4><p>原因：text类型不支持聚合查询（text类型会进行分词，分词后建立索引，支持模糊查询，支持准确查询，不支持聚合查询）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">解决方案：</span><br><span class="line">1.可以通过开启fielddata来解决（在启用fielddata之前，请考虑使用文本字段进行聚合，排序或脚本的原因）</span><br><span class="line"><span class="meta"># </span><span class="language-bash">在ElasticSearch中默认fielddata默认是<span class="literal">false</span>的，因为开启Text的fielddata后对内存的占用很高</span></span><br><span class="line">2.因开启fielddata占用内存很高，所以使用聚类操作是用keywokrd操作（常用做法）</span><br></pre></td></tr></table></figure>
<h4 id="4-2-Elasticsearch-聚合数据结果不精确，怎么破？"><a href="#4-2-Elasticsearch-聚合数据结果不精确，怎么破？" class="headerlink" title="4.2 Elasticsearch 聚合数据结果不精确，怎么破？"></a>4.2 Elasticsearch 聚合数据结果不精确，怎么破？</h4><p>前提认知，Elasticsearch  terms分桶聚合结果是不准确的</p>
<p>导致聚合不精确的原因分析：</p>
<ul>
<li><p><strong>效率因素</strong>：每个分片的取值Top X，并不是汇总全部的 TOP X。</p>
</li>
<li><p><strong>性能因素</strong>：ES 可以不每个分片Top X，而是全量聚合，但势必这会有很大的性能问题。</p>
</li>
</ul>
<p>如何提高聚合精准度：</p>
<p>方案1：设置主分片为1</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">适用场景：数据量小小集群规模业务场景。</span><br></pre></td></tr></table></figure>
<p>方案2：调大 shard_size 值</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">设置 shard_size 为比较大的值，官方推荐：size*1.5+10</span><br><span class="line">适用场景：数据量大、分片数多的集群业务场景.</span><br><span class="line">shard_size 值越大，结果越趋近于精准聚合结果值。</span><br><span class="line">此外，还可以通过show_term_doc_count_error参数显示最差情况下的错误值，用于辅助确定 shard_size 大小。</span><br></pre></td></tr></table></figure>
<p>方案3：将size设置为全量值，来解决精度问题</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">将size设置为2的32次方减去1也就是分片支持的最大值，来解决精度问题</span><br><span class="line">原因：1.x版本，size等于 0 代表全部，高版本取消 0 值，所以设置了最大值（大于业务的全量值）。</span><br><span class="line">全量带来的弊端就是：如果分片数据量极大，这样做会耗费巨大的CPU 资源来排序，而且可能会阻塞网络。</span><br><span class="line">适用场景：对聚合精准度要求极高的业务场景，由于性能问题，不推荐使用。</span><br></pre></td></tr></table></figure>
<p>方案4：使用Clickhouse 进行精准聚合</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">聚合这块比较看场景，多维分析，ES并不是特别擅长。如果有丰富的多维分析场景，还有比较高的性能要求。可以调研下clickhouse。我们这边测评过开源和内部的 大部分场景 clickhouse 几十亿的级别，基本也在秒级返回甚至毫秒级。</span><br><span class="line">此外，除了 chlickhouse， spark也有类似聚合的功能。</span><br><span class="line">适用场景：数据量非常大、聚合精度要求高、响应速度快的业务场景。</span><br><span class="line">																			-- csdn文章中解决方案（暂时没有了解）</span><br></pre></td></tr></table></figure>
<p>小结：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">设置10和大于10将会导致聚合结果不一样是由于 Elasticsearch 聚合实现机制决定的，不是Bug。Elasticsearch本身不提供精准分桶聚合。要提高聚合精度，参考文章提到的几种方案。</span><br></pre></td></tr></table></figure>
<h4 id="4-3-ES查询超过10000条就会报错"><a href="#4-3-ES查询超过10000条就会报错" class="headerlink" title="4.3 ES查询超过10000条就会报错"></a>4.3 ES查询超过10000条就会报错</h4><p>前提认知：在使用es进行数据查询时，由于es官方默认限制了索引一次性最多只能查询10000条数据，查询第10001条数据开始就会报错</p>
<p>解决方案：</p>
<p>方案1：在kibana中执行,解除索引最大查询数的限制</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">put _all/_settings</span><br><span class="line">&#123;</span><br><span class="line">	&quot;index.max_result_window&quot;:200000</span><br><span class="line">&#125;</span><br><span class="line">_all表示所有索引，针对单个索引的话修改成索引名称即可</span><br></pre></td></tr></table></figure>
<p>方案2：在创建索引的时候加上</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">“settings”:&#123;</span><br><span class="line">	&quot;index&quot;:&#123;</span><br><span class="line">		&quot;max_result_window&quot;: 500000</span><br><span class="line">	&#125;</span><br><span class="line">&#125;	</span><br><span class="line">但是修改完之后，通过api查询回来的totalhits还是只有10000条，解决如下 :</span><br><span class="line">在查询时候把 track_total_hits 设置为 true。</span><br><span class="line">track_total_hits 设置为false禁用跟踪匹配查询的总点击次数</span><br><span class="line">设置为true就会返回真实的命中条数</span><br><span class="line">java代码在构建条件时候加上:</span><br><span class="line">searchSourceBuilder.trackTotalHits(true);</span><br></pre></td></tr></table></figure>
<p>ES的深分页需要处理大量的数据，计算复杂度较高，性能会受到极大影响，因此ES底层对查询的数据量做了限制，不建议使用该种方式处理大量数据。</p>
<p>如不需要对所有数据进行查询可以限制展示条数只查询到10000条</p>
<p>另：因ES的深分页需要处理大量的数据，计算复杂度较高，使用from+size浅分页内存消耗非常大，速度一般，如大数据量查询分页尽量不使用（可以使用scroll或search after，但不提供跳转页面功能，只能下一页的翻页，并不适用于有跳页的情景）</p>
<h3 id="5-ES滚动查询"><a href="#5-ES滚动查询" class="headerlink" title="5.ES滚动查询"></a>5.ES滚动查询</h3><h4 id="5-1-elasticsearch之scroll滚动查询（注：在7-x官方文档中，已不太建议使用Scroll-API来深分页了）"><a href="#5-1-elasticsearch之scroll滚动查询（注：在7-x官方文档中，已不太建议使用Scroll-API来深分页了）" class="headerlink" title="5.1 elasticsearch之scroll滚动查询（注：在7.x官方文档中，已不太建议使用Scroll API来深分页了）"></a>5.1 elasticsearch之scroll滚动查询（<strong>注：在7.x官方文档中，已不太建议使用Scroll API来深分页了</strong>）</h4><p>滚动查询意思指的是如果获取的数据特别多，那我们可以分批获取数据，给服务器一个处理请求的时间，一次性查询出来数据，分批次进行获取，所以每次进行查询的时候都要传递一个系统的处理时间<br>第一进行查询的时候 需要指定scroll = time 以及传递 每次获取的条数即可</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">GET goods/_search?scroll=1m</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;match_all&quot;: &#123;&#125;</span><br><span class="line">  &#125;</span><br><span class="line">  , &quot;size&quot;: 20</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>srocll滚动查询的时候，系统会给我们返回一个scroll_id 系统保存了我们上次查询的快照，<br>以后每次获取新的数据的时候都需要传递这个 scroll_id 就可以获取接下来的数据<br>注意：scroll的时间不能设置的太短，也不能设置的太久 es7版本的时候，每次返回的是同一个scroll_id</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">POST _search/scroll</span><br><span class="line">&#123;</span><br><span class="line">  &quot;scroll&quot; : &quot;1m&quot;</span><br><span class="line">  ,&quot;scroll_id&quot; : &quot;DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAMXFAWcmRUUlp6VmxRd0tlMEpXbllLeXlsQQ==&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果一次性要查出来比如10万条数据，那么性能会很差，此时一般会采取用scoll滚动查询，一批一批的查，直到所有数据都查询完处理完</p>
<p>使用scoll滚动搜索，可以先搜索一批数据，然后下次再搜索一批数据，以此类推，直到搜索出全部的数据来<br>scoll搜索会在第一次搜索的时候，保存一个当时的视图快照，之后只会基于该旧的视图快照提供数据搜索，如果这个期间数据变更，是不会让用户看到的<br>采用基于_doc进行排序的方式，性能较高<br>每次发送scroll请求，我们还需要指定一个scoll参数，指定一个时间窗口，每次搜索请求只要在这个时间窗口内能完成就可以了</p>
<p>scoll，看起来挺像分页的，但是其实使用场景不一样。分页主要是用来一页一页搜索，给用户看的；scoll主要是用来一批一批检索数据，让系统进行处理的</p>
<p>scroll滚动查询推荐在报表全部导出是使用</p>
<p>游标过期时间</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">启用游标查询可以通过在查询的时候设置参数 scroll 的值为我们期望的游标查询的过期时间。 游标查询的过期时间会在每次做查询的时候刷新，所以这个时间只需要足够处理当前批的结果就可以了，而不是处理查询结果的所有文档的所需时间</span><br><span class="line"></span><br><span class="line">这个时间其实指的是es把本次快照的结果缓存起来的有效时间。</span><br><span class="line">scroll 参数相当于告诉了 ES我们的search context要保持多久，后面每个 scroll 请求都会设置一个新的过期时间，以确保我们可以一直进行下一页操作。</span><br></pre></td></tr></table></figure><script type="text&#x2F;javascript" src="https://unpkg.com/kity@2.0.4/dist/kity.min.js"></script><script type="text&#x2F;javascript" src="https://unpkg.com/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text&#x2F;javascript" src="https://unpkg.com/hexo-simple-mindmap@0.8.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text&#x2F;css" href="https://unpkg.com/hexo-simple-mindmap@0.8.0/dist/mindmap.min.css"></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">HustleLr</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://greenfivebird.gitee.io/hustleblog/2022/03/01/es%E5%85%A5%E9%97%A8/">http://greenfivebird.gitee.io/hustleblog/2022/03/01/es%E5%85%A5%E9%97%A8/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hustleblog/tags/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a></div><div class="post_share"><div class="social-share" data-image="/hustleblog/img/es.jfif" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/hustleblog/2022/03/01/Kafka%E5%85%A5%E9%97%A8/"><img class="prev-cover" src="/hustleblog/img/kafka.jfif" onerror="onerror=null;src='/hustleblog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">Kafka入门</div></div></a></div><div class="next-post pull-right"><a href="/hustleblog/2022/03/01/redis%E9%9B%86%E7%BE%A4/"><img class="next-cover" src="/hustleblog/img/redis.jfif" onerror="onerror=null;src='/hustleblog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">redis集群</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/hustleblog/2022/03/01/Kafka%E5%85%A5%E9%97%A8/" title="Kafka入门"><img class="cover" src="/hustleblog/img/kafka.jfif" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-01</div><div class="title">Kafka入门</div></div></a></div><div><a href="/hustleblog/2022/03/06/docker%E6%93%8D%E4%BD%9C/" title="docker操作"><img class="cover" src="/hustleblog/img/docker.webp" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-06</div><div class="title">docker操作</div></div></a></div><div><a href="/hustleblog/2021/06/23/kafka%E9%9D%A2%E8%AF%95%E9%A2%98/" title="kafka面试题"><img class="cover" src="/hustleblog/img/question.jfif" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-23</div><div class="title">kafka面试题</div></div></a></div><div><a href="/hustleblog/2021/04/16/redis%E9%9D%A2%E8%AF%95%E9%A2%98/" title="redis面试题"><img class="cover" src="/hustleblog/img/question.jfif" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-16</div><div class="title">redis面试题</div></div></a></div><div><a href="/hustleblog/2022/03/03/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/" title="分布式锁"><img class="cover" src="/hustleblog/img/suo.jfif" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-03</div><div class="title">分布式锁</div></div></a></div><div><a href="/hustleblog/2022/03/01/redis%E9%9B%86%E7%BE%A4/" title="redis集群"><img class="cover" src="/hustleblog/img/redis.jfif" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-01</div><div class="title">redis集群</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/hustleblog/img/head.jpg" onerror="this.onerror=null;this.src='/hustleblog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">HustleLr</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/hustleblog/archives/"><div class="headline">Articles</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/hustleblog/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a></div><div class="card-info-data-item"><a href="/hustleblog/categories/"><div class="headline">Categories</div><div class="length-num">12</div></a></div></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">Welcome to Wlr's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-ElasticSearch%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">1. ElasticSearch基本概念</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-ES%E7%B4%A2%E5%BC%95"><span class="toc-number">1.1.</span> <span class="toc-text">1.1 ES索引</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-%E6%96%87%E6%A1%A3%EF%BC%88Document%EF%BC%89"><span class="toc-number">1.2.</span> <span class="toc-text">1.2 文档（Document）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-%E5%AD%97%E6%AE%B5%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.</span> <span class="toc-text">1.3  字段类型</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-ES%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="toc-number">2.</span> <span class="toc-text">2. ES基本操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-%E5%B8%B8%E7%94%A8%E7%B4%A2%E5%BC%95%E6%93%8D%E4%BD%9C"><span class="toc-number">2.1.</span> <span class="toc-text">2.1 常用索引操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-%E5%B8%B8%E7%94%A8%E6%96%87%E6%A1%A3%E6%93%8D%E4%BD%9C"><span class="toc-number">2.2.</span> <span class="toc-text">2.2 常用文档操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-%E5%B8%B8%E7%94%A8%E6%9F%A5%E8%AF%A2ES%E5%86%85%E5%AE%B9%E6%93%8D%E4%BD%9C%EF%BC%88%E5%8F%AF%E4%BD%BF%E7%94%A8postman%E6%88%96%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9B%B4%E6%8E%A5%E6%9F%A5%E8%AF%A2%EF%BC%89"><span class="toc-number">2.3.</span> <span class="toc-text">2.3 常用查询ES内容操作（可使用postman或浏览器直接查询）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-4-DSL%E8%AF%AD%E8%A8%80%E9%AB%98%E7%BA%A7%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.4.</span> <span class="toc-text">2.4 DSL语言高级查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-term%E4%B8%8Ematch"><span class="toc-number">2.5.</span> <span class="toc-text">2.5    term与match</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-JavaAPI%E6%95%B4%E5%90%88ES"><span class="toc-number">3.</span> <span class="toc-text">3. JavaAPI整合ES</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-java%E9%9B%86%E6%88%90ES%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 java集成ES代码示例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E7%B2%BE%E7%A1%AE%E6%9F%A5%E8%AF%A2A"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 精确查询A</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-%E6%A8%A1%E7%B3%8A%E6%9F%A5%E8%AF%A2"><span class="toc-number">3.3.</span> <span class="toc-text">3.3 模糊查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-%E8%8C%83%E5%9B%B4%E6%9F%A5%E8%AF%A2"><span class="toc-number">3.4.</span> <span class="toc-text">3.3 范围查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-%E5%A4%9A%E4%B8%AA%E5%85%B3%E9%94%AE%E5%AD%97%E7%BB%84%E5%90%88%E6%9F%A5%E8%AF%A2boolQuery"><span class="toc-number">3.5.</span> <span class="toc-text">3.4 多个关键字组合查询boolQuery()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-%E8%81%9A%E5%90%88%E6%93%8D%E4%BD%9C"><span class="toc-number">3.6.</span> <span class="toc-text">3.5 聚合操作</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-ES%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98"><span class="toc-number">4.</span> <span class="toc-text">4.ES常见问题</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-Es%E7%9A%84%E8%81%9A%E5%90%88%EF%BC%8C%E7%BB%9F%E8%AE%A1%E6%9F%A5%E8%AF%A2%E6%97%B6%E6%8A%A5%E9%94%99fielddata-true%E9%97%AE%E9%A2%98"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 Es的聚合，统计查询时报错fielddata&#x3D;true问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-Elasticsearch-%E8%81%9A%E5%90%88%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%9C%E4%B8%8D%E7%B2%BE%E7%A1%AE%EF%BC%8C%E6%80%8E%E4%B9%88%E7%A0%B4%EF%BC%9F"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 Elasticsearch 聚合数据结果不精确，怎么破？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-ES%E6%9F%A5%E8%AF%A2%E8%B6%85%E8%BF%8710000%E6%9D%A1%E5%B0%B1%E4%BC%9A%E6%8A%A5%E9%94%99"><span class="toc-number">4.3.</span> <span class="toc-text">4.3 ES查询超过10000条就会报错</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-ES%E6%BB%9A%E5%8A%A8%E6%9F%A5%E8%AF%A2"><span class="toc-number">5.</span> <span class="toc-text">5.ES滚动查询</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-elasticsearch%E4%B9%8Bscroll%E6%BB%9A%E5%8A%A8%E6%9F%A5%E8%AF%A2%EF%BC%88%E6%B3%A8%EF%BC%9A%E5%9C%A87-x%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3%E4%B8%AD%EF%BC%8C%E5%B7%B2%E4%B8%8D%E5%A4%AA%E5%BB%BA%E8%AE%AE%E4%BD%BF%E7%94%A8Scroll-API%E6%9D%A5%E6%B7%B1%E5%88%86%E9%A1%B5%E4%BA%86%EF%BC%89"><span class="toc-number">5.1.</span> <span class="toc-text">5.1 elasticsearch之scroll滚动查询（注：在7.x官方文档中，已不太建议使用Scroll API来深分页了）</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/hustleblog/2024/04/21/%E4%BB%A3%E7%A0%81%E9%9A%8F%E6%83%B3%E5%BD%95Day-2/" title="代码随想录Day 2"><img src="/hustleblog/img/code.png" onerror="this.onerror=null;this.src='/hustleblog/img/404.jpg'" alt="代码随想录Day 2"/></a><div class="content"><a class="title" href="/hustleblog/2024/04/21/%E4%BB%A3%E7%A0%81%E9%9A%8F%E6%83%B3%E5%BD%95Day-2/" title="代码随想录Day 2">代码随想录Day 2</a><time datetime="2024-04-20T16:31:56.000Z" title="Created 2024-04-21 00:31:56">2024-04-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hustleblog/2024/04/17/%E4%BB%A3%E7%A0%81%E9%9A%8F%E6%83%B3%E5%BD%95Day-1/" title="代码随想录Day 1"><img src="/hustleblog/img/code.png" onerror="this.onerror=null;this.src='/hustleblog/img/404.jpg'" alt="代码随想录Day 1"/></a><div class="content"><a class="title" href="/hustleblog/2024/04/17/%E4%BB%A3%E7%A0%81%E9%9A%8F%E6%83%B3%E5%BD%95Day-1/" title="代码随想录Day 1">代码随想录Day 1</a><time datetime="2024-04-17T14:26:46.000Z" title="Created 2024-04-17 22:26:46">2024-04-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hustleblog/2022/07/11/Gateway%E8%AE%B0%E5%BD%95/" title="Gateway记录"><img src="/hustleblog/img/gateway.webp" onerror="this.onerror=null;this.src='/hustleblog/img/404.jpg'" alt="Gateway记录"/></a><div class="content"><a class="title" href="/hustleblog/2022/07/11/Gateway%E8%AE%B0%E5%BD%95/" title="Gateway记录">Gateway记录</a><time datetime="2022-07-11T08:28:45.000Z" title="Created 2022-07-11 16:28:45">2022-07-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hustleblog/2022/04/01/%E6%9E%84%E5%BB%BA%E4%BA%8C%E5%8F%89%E6%A0%91/" title="构建二叉树"><img src="/hustleblog/img/%E4%BA%8C%E5%8F%89%E6%A0%91.webp" onerror="this.onerror=null;this.src='/hustleblog/img/404.jpg'" alt="构建二叉树"/></a><div class="content"><a class="title" href="/hustleblog/2022/04/01/%E6%9E%84%E5%BB%BA%E4%BA%8C%E5%8F%89%E6%A0%91/" title="构建二叉树">构建二叉树</a><time datetime="2022-03-31T16:29:49.000Z" title="Created 2022-04-01 00:29:49">2022-04-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/hustleblog/2022/03/22/Java%E5%B8%B8%E8%A7%81%E5%87%A0%E7%A7%8D%E6%8E%92%E5%BA%8F/" title="Java常见几种排序"><img src="/hustleblog/img/sort.webp" onerror="this.onerror=null;this.src='/hustleblog/img/404.jpg'" alt="Java常见几种排序"/></a><div class="content"><a class="title" href="/hustleblog/2022/03/22/Java%E5%B8%B8%E8%A7%81%E5%87%A0%E7%A7%8D%E6%8E%92%E5%BA%8F/" title="Java常见几种排序">Java常见几种排序</a><time datetime="2022-03-22T09:51:45.000Z" title="Created 2022-03-22 17:51:45">2022-03-22</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/hustleblog/img/es.jfif')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2024 By HustleLr</div><div class="footer_custom_text">Welcome to HustleLr's Blog</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Toggle Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/hustleblog/js/utils.js"></script><script src="/hustleblog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="有,志,者,事,竟,成" data-fontsize="15px" data-random="false" async="async"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>